<?php
include "include/config.inc.php";
if(!isset($_SESSION['s_activId']))
{
  header('location:checkLogin.php');
}

$m=0;
$createFrom = isset($_POST['createFrom']) ? $_POST['createFrom'] : 0;

$loanArray = array();
$h=0;
$selectLoan = "SELECT loanId, loan.accountId , loan.value, loanAmount,value,
                      loan.itemImage, itemlocation.itemlocationName, loanDate, monthlyInterest, monthlyInterestRs
                 FROM loan
            LEFT JOIN itemlocation ON loan.itemlocationId = itemlocation.itemlocationId
                WHERE loanId=".$_GET['createFrom'];
$selectLoanRes = mysql_query($selectLoan);
while($loanRow = mysql_fetch_array($selectLoanRes))
{
//	$selectLoanAmount = "SELECT SUM(loanAmount) AS totaLoanAmount
//	                       FROM loan
//	                      WHERE itemId = ".$loanRow['itemId'];
//  $selectLoanAmountRes = mysql_query($selectLoanAmount);
//  $selectLoanAmountRow = mysql_fetch_array($selectLoanAmountRes); 
	$loanArray['loanId']            = $loanRow['loanId'];
	$loanArray['partyId']           = $loanRow['accountId'];
	$loanArray['itemId']            = 0;//$loanRow['itemId'];
	$loanArray['value']             = $loanRow['value'];
	$loanArray['loanAmount']        = $loanRow['loanAmount'];
	$loanArray['itemName']          = 0;//$loanRow['itemName'];
	$loanArray['weight']            = 0;//$loanRow['weight'];
	$loanArray['itemImage']         = $loanRow['itemImage'];
	$loanArray['itemlocationName']  = $loanRow['itemlocationName'];
	$loanArray['loanDate']          = $loanRow['loanDate'];
	$loanArray['monthlyInterest']   = $loanRow['monthlyInterest'];
	$loanArray['monthlyInterestRs'] = $loanRow['monthlyInterestRs'];
	$loanArray['value']             = $loanRow['value'];
}

if(isset($_POST['loanBtn']))
{
  $partyName  = $_POST['partyName'];
	$address    = $_POST['address'];
	$mobile     = $_POST['mobile'];
  $otherInfo  = $_POST['otherInfo'];
  $value      = $_POST['value'];
  $partyId    = $_POST['partyIdtxt'];
	
	
  $updateParty = "UPDATE party 
                     SET partyName   = '".$partyName."',
                         address     = '".$address."',
                         mobile      = '".$mobile."',
  	                     otherInfo   = '".$otherInfo."'
                   WHERE partyId     = ".$_POST['partyIdOld'];
  $updatePartyRes    = mysql_query($updateParty);
  
  $loanDate          = $_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay'];	
  $addOnAmount       = $_POST['addOnAmount'];	
  $itemId            = $_POST['itemIdtxt'];	
  $weight            = $_POST['weight'];	
  $monthlyInterest   = $_POST['monthlyInterest'];	
  $monthlyInterestRs = $_POST['monthlyInterestRs'];	
  $createFromId      = $_POST['createFromId'];
  $itemImage         = $_POST['itemImage'];
  
  $insLoan           = "INSERT INTO `loan` (`partyId`, `loanAmount` , `value`, `itemId`, `loanDate` ,`monthlyInterest`, `monthlyInterestRs`, `createFromId`, `weight`, `itemImage`, `lastInterestReceiveDate`)
                             VALUES ('".$partyId."',
                                     '".$addOnAmount."', 
                                     '".$addOnAmount."', 
                                     '".$itemId."', 
                                     '".$loanDate."', 
                                     '".$monthlyInterest."', 
                                     '".$monthlyInterestRs."', 
                                     '".$createFromId."', 
                                     '".$weight."', 
                                     '".$itemImage."', 
                                     '0000-00-00')";
  $insLoanRes        = mysql_query($insLoan);
  header("Location:loanList.php");
}

if(isset($_POST['createFrom']) > 0)
{
	$currentLoanAmount = isset($_POST['currentLoanAmount']) ? $_POST['currentLoanAmount'] :0;
	echo $updateAddon = "UPDATE loan(currentLoanAmount) values('".$currentLoanAmount."')
	                            WHERE loanId ".$_REQUEST['loanId'];
	$updateAddonRes = mysql_query($updateAddon);
	if(!$updateAddonRes)
	{
		echo "Add On Fail";
	}
	else
	{
		header("Location:loanAddOn.php");
	}
}


$arrParty = array();
$i=0;
$selParty="SELECT `accountId`,`accountName` 
             FROM `account`";
$selPartyRes=mysql_query($selParty);
while($selPartyResRow=mysql_fetch_array($selPartyRes))
{
  $arrParty['partyName'][$i] = $selPartyResRow['accountName'];
  $arrParty['partyId'][$i]   = $selPartyResRow['accountId'];
  $i++;
}
///////////fetch the party Name Over

include("bottom.php");
$smarty->assign('arrParty',$arrParty);  
$smarty->assign('loanArray',$loanArray);  
$smarty->assign('createFrom',$createFrom);  
$smarty->display('loanAddOn.tpl');  
?>